<?php
class ShiftModel extends CI_Model{

    public function __construct(){
        $this->load->database();
        parent:: __construct();

    }

    //手动设置发车时间
    public function setStartTime($userId, $shiftId, $time){
        $data = array(
            'scheduleDate'=>date("Y-m-d H:i:s", $time)
        );
        $where = array('shiftId'=>$shiftId);
        $this->db->update('shiftlist', $data, $where); 
        return $this->db->affected_rows();
    }

    //手动设置到达时间
    public function setArriveTime($userId, $shiftId, $time){

        $data = array(
            'arrivingTime'=>date("Y-m-d H:i:s", $time)
        );
        $where = array('shiftId'=>$shiftId);
        $this->db->update('shiftlist', $data, $where); 
        $rs = $this->db->affected_rows();

        return $this->sendMessage($shiftId);
    }

    //发送消息
    public function sendMessage($shiftId){

        //获取所有formId
        $where = array(
            'shiftId'=>$shiftId,
            'cancelTime'=>null,
        ); 
        $this->db->select('formId');
        $query = $this->db->get_where("reservationlist", $where);
        $queryData = $query->result_array(); 
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

        //先获取token
        curl_setopt($ch, CURLOPT_URL, 
            'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx1fb381ba6beed623&secret=5a851c9eec5c34d656a6c818faa921ee');
        $data = curl_exec($ch);
        if(curl_errno($ch) ==0){
            $data = json_decode($data,true);
            $token = $data["access_token"];

            foreach($queryData as $reserve){
                //推送消息
                curl_setopt($ch, CURLOPT_URL,'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$token); 
                curl_setopt($ch, CURLOPT_POST, 1);
                $postData = array(
                    'touser'=>'opCRN5eTggRUYPZnNz62hoR5MwRY',
                    'template_id'=>'d5kkvjXZS_ZKX_2p-bxaCWMr3eOgSSpKuAHlUWS600w',
                    'form_id'=>$reserve['formId'],
                  //  'form_id'=>'1529935173099',
                    'data'=>array('keyword1'=>"班车已成功抵达,请上传地理位置.")
                );

                $headers = array("Content-type: application/json;charset=UTF-8","Accept: application/json","Cache-Control: no-cache", "Pragma: no-cache");
                $postData=json_encode($postData);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                $sendRs = curl_exec($ch);
                if(curl_errno($ch) ==0){
                    $sendMsg = json_decode($sendRs,true); 
                    if($sendMsg['errcode'] =='0'){
                        $rs = 0;
                    }else{
                        $rs = -1;
                    }
                    return $rs;
                }
            }
        }else{
            return 0;
        }

    }
}
